import demo1.Test1;
class Money implements Cloneable {
    //这里面也要使用克隆接口 和 重写克隆方法
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable {
    public String name;
    public int age;

    public Money money = new Money();

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //为了实现深拷贝
        Person tmp = (Person)super.clone();
        //之后tmp中的money再拷贝一次
        tmp.money = (Money)this.money.clone();
        return tmp;
    }
}

public class Test {
    public static void main(String[] args) {
        Person person1 = new Person("zhangsan", 10);
        Person person2 = new Person("zhangsan", 10);
    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan",10);

        //此时新创将一个对象，并将person1内容拷贝给新对象
        Person person2 = (Person) person1.clone();//此时需要调用克隆方法

        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
        System.out.println("=============");
        person1.money.m = 200;

        System.out.println(person1.money.m);
        System.out.println(person2.money.m);

    }
}


